package org.example.everyday;

import java.sql.SQLOutput;
import java.text.DateFormatSymbols;

/**
 * <p>
 * Description:TODO 详细描述
 * <p>
 * Copyright: 天津联想协同科技有限公司
 * <p>
 *
 * @author anlu
 * @date 2022/3/15 10:14
 */
public class Lc2044 {
    int[] nums;
    int maxOr,cnt;

    public static void main(String[] args) {
        Lc2044 lc2044 = new Lc2044();
        int[] nums = {3,1};
//        System.out.println(1 | 1);



//        int result = lc2044.countMaxOrSubsets(nums);


//        lc2044.test(nums);
       int result =  lc2044.solution2(nums);
                System.out.println(result);
    }


    public int countMaxOrSubsets(int[] nums) {
        int maxOr = 0, cnt = 0;
        for (int i = 0; i < 4; i++) {
            int orVal = 0;
            for (int j = 0; j < nums.length; j++) {
                System.out.println("i="+i+";nums[j]="+nums[j]);
                if (((i >> j) & 1) == 1) {
                    orVal |= nums[j];
                }
            }
            if (orVal > maxOr) {
                maxOr = orVal;
                cnt = 1;
            } else if (orVal == maxOr) {
                cnt++;
            }
        }
        return cnt;
    }

    public int solution2(int[] nums){
        this.nums = nums;
        this.maxOr = 0;
        this.cnt = 0;
        dfs(0,0);
        return cnt;
    }

    private void dfs(int pos, int orVal) {
        if(pos == nums.length){
            if(orVal > maxOr){
                maxOr = orVal;
                cnt = 1;
            }else if(orVal == maxOr){
                cnt++;
            }
            return;
        }
        dfs(pos + 1, orVal | nums[pos]);
        dfs(pos + 1,orVal);
    }


    private void test(int[] nums){
        for (int i = 0; i < 1 << nums.length; i++) {

            for (int j = 0; j < nums.length; j++) {
//                System.out.println("i="+i+";i >> j = "+ (i >> j));
                int result = (i >> j) & 1;
                if(result == 1){
                    System.out.println("j="+j+";  nums[j]="+nums[j]);
                }

            }

        }
    }

}
